package com.tszs.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tszs.biz.CategoryManagementBiz;
import com.tszs.biz.CommodityListBiz;
import com.tszs.biz.PersonalOrderBiz;
import com.tszs.biz.SmsConfigurationBiz;
import com.tszs.entity.CategoryManagement;
import com.tszs.entity.CategoryManagementExample;
import com.tszs.entity.CommodityList;
import com.tszs.entity.CommodityListExample;
import com.tszs.entity.PersonalOrder;
import com.tszs.entity.PersonalOrderExample;
import com.tszs.entity.SmsConfiguration;
import com.tszs.util.CallBack;
import com.tszs.util.Uuid;
import com.tszs.util.projectPath;

import net.sf.json.JSONObject;

/**
 * @类名:CommodityListCpontroller
 * @类描述：商品管理 @创建时间：2019-03-22 13:07:07
 * @创建人：王俊
 * @author Administrator
 *
 */
@Controller
public class CommodityListCpontroller {
	/**
	 * @PersonalOrderBiz
	 */
	@Autowired
	private PersonalOrderBiz personalOrderBiz;
	/**
	 * @CommodityListBiz
	 * @描述:商品管理
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private CommodityListBiz commodityListBiz;
	/**
	 * @CategoryManagementBiz
	 * @描述:类别管理
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private CategoryManagementBiz categoryManagementBiz;
	
	@Autowired
	private SmsConfigurationBiz smsConfigurationBiz;

	/**
	 * @方法名:classifiedQuery
	 * @方法描述：分类管理查询 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/findCateType")
	public void classifiedQuery(HttpServletRequest request, HttpServletResponse response) {
		String cType = request.getParameter("cType");
		CategoryManagement example = new CategoryManagement();
		String cParentlevelid = request.getParameter("cParentlevelid");
		if (cType.equals("1")) {
			example.setcType(cType);
		} else if (cType.equals("2")) {
			example.setcType(cType);
			example.setcParentlevelid(cParentlevelid);
		} else {
			example.setcType(cType);
			example.setcParentlevelid(cParentlevelid);
		}
		new CallBack().yes(response, categoryManagementBiz.findCateType(example), "");
	}

	/**
	 * @方法名:insertCommodit
	 * @方法描述：商品添加 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/insertCommodit")
	public void insertCommodit(HttpServletRequest request, HttpServletResponse response) {
		try {
			CommodityList record = new CommodityList();
			record.setcId(Uuid.getUUID());
			record.setcName(request.getParameter("cName"));
			record.setcPrice(request.getParameter("cPrice"));
			record.setcType(request.getParameter("cType"));
			record.setcDescribe(request.getParameter("cDescribe"));
			record.setcReateTime(Uuid.getTime());
			record.setcPhtoto(request.getParameter("cPhoto"));
			record.setcStatic("0");
			record.setcNum(request.getParameter("cNum"));
			record.setcAddPeople(request.getSession().getAttribute("aUsername") + "");
			record.setcAddHumanId(request.getSession().getAttribute("aId") + "");
			record.setcPhtotoDescribe(request.getParameter("cPhotodatails"));
			commodityListBiz.insertSelective(record);
			new CallBack().yes(response, "", "");
		} catch (Exception e) {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @方法名:findCommodityListPage
	 * @方法描述：商品列表分页 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/findCommodityListPage")
	public void findCommodityListPage(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String cName = request.getParameter("type2");
		String begin = request.getParameter("type3");
		String typeId = request.getParameter("cateId");
		String price = request.getParameter("price");
		CommodityList example = new CommodityList();
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		example.setcType(typeId);
		example.setcName(cName);
		example.setBegin(begin);
		example.setcPrice(price);
		List<CommodityList> selectByExample = commodityListBiz.selectByExamplePage(example);

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", commodityListBiz.countAllNum());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			String b = "";
			for (int c = 0; c < selectByExample.get(i).getcPhtoto().split(",").length; c++) {
				b += "<a target=\"_Blank\" href='" + projectPath.url + selectByExample.get(i).getcPhtoto().split(",")[c]
						+ "'><img src='" + projectPath.url + selectByExample.get(i).getcPhtoto().split(",")[c]
						+ "'/></a>";
			}
			mapData.put("cPhoto", b);
			mapData.put("cId", i + 1);
			mapData.put("Cname", selectByExample.get(i).getcName());
			mapData.put("cType", selectcId(selectByExample.get(i).getcType()));
			mapData.put("cNum", selectByExample.get(i).getcNum());

			if (selectByExample.get(i).getcStatic().equals("0")) {
				mapData.put("cStatic", "<a style=\"color:green\">上架</a>");
			} else {
				mapData.put("cStatic", "<a style=\"color:red\">下架</a>");
			}

			String append = "";

			if (selectByExample.get(i).getcStatic().equals("0")) {
				append = " <a target=\"_Blank\" href=\"javascript:table('2','" + selectByExample.get(i).getcId()
						+ "')\" class=\"layui-btn layui-btn-danger layui-btn-xs\" style=''>下架</a>";
			} else {
				append = " <a target=\"_Blank\" href=\"javascript:table('3','" + selectByExample.get(i).getcId()
						+ "')\" class=\"layui-btn layui-btn-primary layui-btn-xs\" style=''>已下</a>";
			}
			mapData.put("button", " <a href=\"javascript:table('0','" + selectByExample.get(i).getcId()
					+ "')\" class=\"layui-btn layui-btn-warm layui-btn-xs\" style=''>详情</a>" + append);
			mapData.put("cReateTime", selectByExample.get(i).getcReateTime());
			mapData.put("cPrice", selectByExample.get(i).getcPrice());
			mapData.put("cDescribe", selectByExample.get(i).getcDescribe());
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}

	/**
	 * @方法名称
	 * @方法描述
	 * @param cId
	 *            根据分类cID
	 * @return 分类名称
	 */
	public String selectcId(String cId) {
		CategoryManagement example = new CategoryManagement();
		example.setcId(cId);
		List<CategoryManagement> findCateCid = categoryManagementBiz.findCateCid(example);
		return findCateCid.size() <= 0 ? "" : findCateCid.get(0).getcName();
	}

	/**
	 * @方法名:modifyCommodity
	 * @方法描述：商品修改查询 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/modifyCommodity")
	public void modifyCommodity(HttpServletRequest request, HttpServletResponse response) {
		CommodityListExample example = new CommodityListExample();
		com.tszs.entity.CommodityListExample.Criteria ca = example.createCriteria();
		ca.andCIdEqualTo(request.getParameter("id"));
		List<CommodityList> selectByExample = commodityListBiz.selectByExample(example);
		if (selectByExample.size() > 0) {
			new CallBack().yes(response, selectByExample, "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @方法名:updateCommodity
	 * @方法描述：商品修改 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/updateCommodity")
	public void updateCommodity(HttpServletRequest request, HttpServletResponse response) {
		CommodityList record = new CommodityList();
		
		record.setcId(request.getParameter("cId"));
		record.setcName(request.getParameter("cName"));
		record.setcPrice(request.getParameter("cPrice"));
		record.setcPhtoto(request.getParameter("cPhoto"));
		record.setcNum(request.getParameter("cNum"));
		record.setcDescribe(request.getParameter("cDescribe"));
		record.setcPhtotoDescribe(request.getParameter("cPhtotoDescribe"));
		
		CommodityListExample example = new CommodityListExample();
		com.tszs.entity.CommodityListExample.Criteria ca = example.createCriteria();
		ca.andCIdEqualTo(request.getParameter("cId"));
		int size = commodityListBiz.updateByExampleSelective(record, example);
		if (size > 0) {
			new CallBack().yes(response, size, "");
		} else {
			new CallBack().no(response, size, "");
		}
	}

	/**
	 * @方法名:updateCommodityBycStatic
	 * @方法描述：商品伪删除 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/updateCommodityBycStatic")
	public void updateCommodityBycStatic(HttpServletRequest request, HttpServletResponse response) {
		String type = request.getParameter("type");
		CommodityList record = new CommodityList();
		if (type.equals("2")) {
			record.setcStatic("1");
		} else if (type.equals("3")) {
			record.setcStatic("0");
		} else {
			record.setcStatic("2");
		}
		CommodityListExample example = new CommodityListExample();
		
		record.setcId(request.getParameter("id"));
		
		int size = commodityListBiz.updateByExampleSelective(record, example);
		if (size > 0) {
			new CallBack().yes(response, size, "");
		} else {
			new CallBack().no(response, size, "");
		}
	}

	/**
	 * @方法名:findCommodityListPage
	 * @方法描述：商品列表分页 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/findCommodityListPagecStatic")
	public void findCommodityListPagecStatic(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		String cName = request.getParameter("type2");
		String begin = request.getParameter("type3");
		String cateId = request.getParameter("cateId");
		String price = request.getParameter("price");
		CommodityList example = new CommodityList();
		
		example.setcName(cName);
		example.setBegin(begin);
		example.setcPrice(price);
		example.setcType(cateId);
		example.setPage((page - 1) * limit);
		example.setLimit(limit);

		List<CommodityList> selectByExample = commodityListBiz.selectByExamplePage2(example);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", commodityListBiz.countUndercarriage());

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			String b = "";
			for (int c = 0; c < selectByExample.get(i).getcPhtoto().split(",").length; c++) {
				b += "<a target=\"_Blank\" href='" + projectPath.url + selectByExample.get(i).getcPhtoto().split(",")[c]
						+ "'><img src='" + projectPath.url + selectByExample.get(i).getcPhtoto().split(",")[c]
						+ "'/></a>";
			}
			mapData.put("cPhoto", b);
			mapData.put("cId", i + 1);
			mapData.put("Cname", selectByExample.get(i).getcName());
			mapData.put("cType", selectcId(selectByExample.get(i).getcType()));
			mapData.put("cNum", selectByExample.get(i).getcNum());

			String apend = " <a href=\"javascript:table('1','" + selectByExample.get(i).getcId()
					+ "')\" class=\"layui-btn layui-btn-danger layui-btn-xs\" style=''>删除</a>";

			mapData.put("cStatic", "<a style=\"color:red\">下架</a>");
			mapData.put("button", " <a href=\"javascript:table('3','" + selectByExample.get(i).getcId()
					+ "')\" class=\"layui-btn layui-btn-primary layui-btn-xs\" style=''>上架</a>" + apend);

			mapData.put("cReateTime", selectByExample.get(i).getcReateTime());
			mapData.put("cPrice", selectByExample.get(i).getcPrice());
			mapData.put("cDescribe", selectByExample.get(i).getcDescribe());

			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}

	/***
	 * @方法名：titleShow
	 * @方法描述：前台首页展示
	 * @param response
	 */
	@RequestMapping("txr/titleShow")
	public void titleShow(HttpServletResponse response, HttpServletRequest request) {
		CategoryManagement example = new CategoryManagement();
		List<CategoryManagement> selectByExample = categoryManagementBiz.titleShow(example);
		List<Map<String, Object>> list = new ArrayList<>();
		if (selectByExample.size() > 0) {
			for (int i = 0; i < selectByExample.size(); i++) {
				Map<String, Object> mpas = new HashMap<>();
				mpas.put("pid", selectByExample.get(i).getcId());
				mpas.put("pphoto", selectByExample.get(i).getcPhoto());
				mpas.put("cname", selectByExample.get(i).getcName());
				List<Map<String, String>> list2 = new ArrayList<>();
				List<CommodityList> titleShowall = titleShowall(selectByExample.get(i).getcId());
				for (int j = 0; j < titleShowall.size() && j < 3; j++) {
					Map<String, String> map = new HashMap<>();
					map.put("sid", titleShowall.get(j).getcId());
					map.put("sname", titleShowall.get(j).getcName());
					map.put("sphoto", titleShowall.get(j).getcPhtoto());
					map.put("sprice", titleShowall.get(j).getcPrice());
					list2.add(map);
				}
				mpas.put("datas", list2);
				list.add(mpas);
			}
			new CallBack().yes(response, list, "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/***
	 * @方法名：titleShow2
	 * @方法描述：子页面
	 * @param response
	 */
	@RequestMapping("txr/titleShow2")
	public void titleShow2(HttpServletResponse response, HttpServletRequest request) {
		String cid = request.getParameter("CParentlevelidEqualTo");
		if (cid.equals("0")) {
			CommodityList example = new CommodityList();
			List<CommodityList> selectByExample = commodityListBiz.titleShowshopDatails(example);
			if (selectByExample.size() > 0) {
				new CallBack().yes(response, selectByExample, "");
			} else {
				new CallBack().no(response, "", "");
			}
		} else {
			List<CommodityList> titleShowall = titleShowall(cid);
			if (titleShowall.size() > 0) {
				new CallBack().yes(response, titleShowall, "");
			} else {
				new CallBack().no(response, "", "");
			}
		}

	}

	public List<CommodityList> titleShowall(String cType) {
		CommodityList example = new CommodityList();
		example.setcType("0".equals(cType) ? null : cType);
		List<CommodityList> titleShowall = commodityListBiz.titleShow(example);
		return titleShowall;
	}

	/***
	 * @方法名：titleShow
	 * @方法描述：前台首页展示10个最新商品
	 * @param response
	 */
	@RequestMapping("txr/titleShowall")
	public void titleShowall(HttpServletResponse response, HttpServletRequest request) {
		CommodityList example = new CommodityList();
		example.setcType(request.getParameter("cid"));
		example.setcStatic("0");
		List<CommodityList> selectByExample = commodityListBiz.titleShowall(example);
		List<CommodityList> list = new ArrayList<>();
		for (int i = 0; i < selectByExample.size(); i++) {
			CommodityList commodityList = new CommodityList();
			commodityList.setcId(selectByExample.get(i).getcId());
			commodityList.setcName(selectByExample.get(i).getcName()
					+ "<br><a style=\"width: 95%;float: left;height: 40px;line-height: 20px;font-size: 12px;color: #666;\">"
					+ selectByExample.get(i).getcDescribe() + "<a>");
			commodityList.setcPrice(selectByExample.get(i).getcPrice());
			commodityList.setcPhtoto(selectByExample.get(i).getcPhtoto());
			list.add(commodityList);
		}
		new CallBack().yes(response, list, "");
	}

	/***
	 * @方法名：findByIdDatails
	 * @方法描述：根据ID找到商品的所有信息
	 * @param response
	 */
	@RequestMapping("txr/findByIdDatails")
	public void findByIdDatails(HttpServletResponse response, HttpServletRequest request) {
		String cid = request.getParameter("cid");
		CommodityListExample example = new CommodityListExample();
		com.tszs.entity.CommodityListExample.Criteria ca = example.createCriteria();
		ca.andCIdEqualTo(cid);
		List<CommodityList> selectByExample = commodityListBiz.selectByExample(example);

		PersonalOrderExample personalOrderExample = new PersonalOrderExample();
		com.tszs.entity.PersonalOrderExample.Criteria ca2 = personalOrderExample.createCriteria();
		ca2.andProductIdEqualTo(cid);
		ca2.andUIdEqualTo(request.getSession().getAttribute("openid") + "");
		ca2.andPStaticEqualTo("0");
		ca2.andPayStaticEqualTo("0");
		List<PersonalOrder> selectByExamplepersonalOrderExample = personalOrderBiz
				.selectByExample(personalOrderExample);
		String nums = "";
		if (selectByExamplepersonalOrderExample.size() > 0) {
			nums = selectByExamplepersonalOrderExample.get(0).getProductNum();
		}
		
		String value = "在线客服";
		
		SmsConfiguration sms = smsConfigurationBiz.queryOnlineService(value);
		String online = sms == null ? "":sms.getsConten();
		
		List<CommodityList> list = new ArrayList<>();
		for (int i = 0; i < selectByExample.size(); i++) {
			CommodityList commodityList = new CommodityList();
			commodityList.setcDescribe(selectByExample.get(i).getcDescribe());
			commodityList.setcId(selectByExample.get(i).getcId());
			commodityList.setcName(selectByExample.get(i).getcName());
			String b = "";
			for (int c = 0; c < selectByExample.get(i).getcPhtoto().split(",").length; c++) {
				b += "<li><a class=\"pic\" href=\"#\"><img src='" + projectPath.url
						+ selectByExample.get(i).getcPhtoto().split(",")[c] + "'/></a></li>";
			}
			commodityList.setcPhtoto(b);
			commodityList.setcPhtotoDescribe(projectPath.url + selectByExample.get(i).getcPhtotoDescribe());
			commodityList.setcPrice(selectByExample.get(i).getcPrice());
			commodityList.setcNum(nums);
			commodityList.setOnlineService(online);
			list.add(commodityList);
		}
		new CallBack().yes(response, list, "");
	}

	@RequestMapping("txr/findShopDatails")
	public void findShopDatails(HttpServletRequest request, HttpServletResponse response) {
		String cid = request.getParameter("cid");
		if (cid.equals("0")) {
			//所有商品
			CategoryManagementExample example = new CategoryManagementExample();
			com.tszs.entity.CategoryManagementExample.Criteria ca = example.createCriteria();
			ca.andCStaticNotEqualTo("1");
			List<CategoryManagement> selectByExample = categoryManagementBiz.selectByExample(example);
			List<Map<String, Object>> list = new ArrayList<>();
			if (selectByExample.size() > 0) {
				for (int i = 0; i < selectByExample.size(); i++) {
					Map<String, Object> mpas = new HashMap<>();
					mpas.put("pphoto", selectByExample.get(i).getcPhoto());
					List<Map<String, String>> list2 = new ArrayList<>();
					List<CommodityList> titleShowall = titleShowall(selectByExample.get(i).getcId());
					for (int j = 0; j < titleShowall.size(); j++) {
						Map<String, String> map = new HashMap<>();
						map.put("sid", titleShowall.get(j).getcId());
						map.put("sname", titleShowall.get(j).getcName());
						map.put("sphoto", titleShowall.get(j).getcPhtoto());
						map.put("sprice", titleShowall.get(j).getcPrice());
						list2.add(map);
					}
					mpas.put("datas", list2);
					list.add(mpas);
				}
				new CallBack().yes(response, list, "");
			} else {
				new CallBack().no(response, "", "");
			}
		} else {
			
			//类别商品
			CategoryManagementExample example = new CategoryManagementExample();
			com.tszs.entity.CategoryManagementExample.Criteria ca = example.createCriteria();
			ca.andCStaticEqualTo("0");//上架产品
			ca.andCParentlevelidEqualTo(cid);
			List<CategoryManagement> selectByExample = categoryManagementBiz.selectByExample(example);
			List<Map<String, Object>> list = new ArrayList<>();
			if (selectByExample.size() > 0) {
				for (int i = 0; i < selectByExample.size(); i++) {
					Map<String, Object> mpas = new HashMap<>();
					mpas.put("pphoto", selectByExample.get(i).getcPhoto());
					List<Map<String, String>> list2 = new ArrayList<>();
					List<CommodityList> titleShowall = titleShowall(selectByExample.get(i).getcId());
					for (int j = 0; j < titleShowall.size(); j++) {
						Map<String, String> map = new HashMap<>();
						map.put("sid", titleShowall.get(j).getcId());
						map.put("sname", titleShowall.get(j).getcName());
						map.put("sphoto", titleShowall.get(j).getcPhtoto());
						map.put("sprice", titleShowall.get(j).getcPrice());
						list2.add(map);
					}
					mpas.put("datas", list2);
					list.add(mpas);
				}
				new CallBack().yes(response, list, "");
			} else {
				new CallBack().no(response, "", "");
			}
		}

	}
}
